using System.Collections.Generic;
using UnityEngine;

public class TurnManager:MonoBehaviour
{
    public static TurnManager instance;
    private void Awake()
    {
        instance=this;
    }

    [Header("符咒")]
    public List<TurnDatas> datas = new List<TurnDatas>();
    [Header("是否拥有")]
    public List<bool> haveItem = new List<bool>();
    [Header("佩戴1")]
    public string currOne = "";
    public string currTow = "";
    [Header("攻击增益表")]
    public Dictionary<string, int> damageM = new Dictionary<string, int>();
    [Header("防御增益表")]
    public Dictionary<string,int> armorM = new Dictionary<string, int>();
    [Header("血量增益表")]
    public Dictionary<string, int> hpM = new Dictionary<string, int>();

    //进行佩戴(名称，装备吗，第一个格子吗)
    public void SetTurn(string str,bool isSet,bool isOne)
    {
        if(isOne)
        {
            if(isSet)
            {
                currOne=str;
            }
            else
            {
                currOne="";
            }
        }
        else
        {
            if (isSet)
            {
                currTow=str;
            }
            else
            {
                currTow="";
            }
        }
    }
    //查看是否佩戴
    public bool SeekTurn(string str)
    {
        if (haveItem.Count <= 0)
        {
            foreach (var i in datas)
            {
                haveItem.Add(false);
            }
        }
        if (datas.Count <= 0) return false;
        if(haveItem.Count<=0) return false;

        for (int i = 0; i<datas.Count; i++)
        {
            if (datas[i].myName==str&&haveItem[i])
            {
                if (currOne==str||currTow==str)
                {
                    return true;
                }
            }
        }

        return false;
    }
    //是否拥有
    public bool SeekHave(string str)
    {
        for (int i = 0; i<datas.Count; i++)
        {
            if (datas[i].myName==str&&haveItem[i])
            {
                return true;
            }
        }

        return false;
    }
    //添加
    public void AddTurn(string str)
    {
        for(int i=0;i<datas.Count;i++)
        {
            if (datas[i].myName==str)
            {
                haveItem[i]=true;
            }
        }


        AchiManager.instance.UnLockAchi("符咒的力量");

        //检查是否全获取魔戒
        CheckAllTurn();
    }
    //获取
    public TurnDatas GetTurn(string str)
    {
        foreach(var i in datas)
        {
            if(str==i.myName)
            {
                return i;
            }
        }

        return null;
    }
    
    //佩戴
    public void Up(string str,int id)
    {
        if (id==0) currOne=str;
        else currTow=str;

        //更新增益
        UpdateModify();
    }
    //脱下
    public void Down(string str,int id)
    {
        if (id==0) currOne="";
        else currTow="";

        //更新增益
        UpdateModify();
    }

    //更新增益
    public void UpdateModify()
    {
        Player player = GameManager.Instance.player;
        player.damage=player.oriDamage;
        player.armor=player.oriArmor;
        if(player.oriHp<0)
        {
            foreach (var i in hpM.Values)
            {
                float index = i / 100.0f;
                int indexHp = Mathf.RoundToInt(player.currHp * index);
                player.currHp -= indexHp <= 0 ? 0 : indexHp;
            }
        }
        else
        {
            player.currHp = player.oriHp;
        }
        //首先移除所有增益
        damageM.Clear();
        armorM.Clear();
        hpM.Clear();

        //相等flag
        bool tigerFlag=false;
        bool rabbitFlag = false;

        if(SeekTurn("鼠"))
        {

        }
        if(SeekTurn("牛"))
        {
            int index = Mathf.RoundToInt(player.damage*0.15f);
            damageM.Add("牛",index<=1 ? 1 : index);
        }
        if (SeekTurn("虎"))
        {
            tigerFlag=true;
        }
        if (SeekTurn("兔"))
        {
            rabbitFlag=true;
        }
        if (SeekTurn("龙"))
        {
            damageM.Add("龙", player.armor);
        }
        if (SeekTurn("蛇"))
        {

        }
        if (SeekTurn("马"))
        {
            int index = Mathf.RoundToInt(player.currHp*(0.3f));
            hpM.Add("马", index);
        }
        if (SeekTurn("羊"))
        {

        }
        if (SeekTurn("猴"))
        {

        }
        if (SeekTurn("鸡"))
        {
            int ballNum = Mathf.RoundToInt(player.armor*0.05f*GameManager.Instance.ballNum);
            armorM.Add("鸡",ballNum);
        }
        if (SeekTurn("狗"))
        {

        }
        if (SeekTurn("猪"))
        {

        }

        if(rabbitFlag)
        {
            player.currSpeed=player.startSpeed*2;
        }
        else
        {
            player.currSpeed=player.startSpeed;
        }
        //计算总的
        foreach(int i in damageM.Values)
        {
            player.damage+=i;
        }
        foreach (int i in armorM.Values)
        {
            player.armor+=i;
        }
        foreach (int i in hpM.Values)
        {
            player.currHp+=i;
        }
        if (tigerFlag)
        {
            int index = Mathf.RoundToInt(((player.damage+player.armor)*1.2f)*0.5f);
            player.damage=index;
            player.armor=index;
        }
        //更新UI
        GameManager.Instance.UpdateMathData();
    }
    //检查集齐
    public void CheckAllTurn()
    {
        bool isAll = true;
        foreach(var i in haveItem)
        {
            if(i==false)
            {
                isAll = false;
                break;
            }
        }

        if(isAll)
        {
            AchiManager.instance.UnLockAchi("集齐符咒！");
        }
    }
}

[System.Serializable]
public class TurnDatas
{
    [Header("名称")]
    public string myName;
    [Header("描述")]
    public string des;
    [Header("图片")]
    public Sprite icon;
}